home *** CD-ROM | disk | FTP | other *** search
- goto example1
- ' The QuickBASIC Tutorial Series
-
- ' Part 2
- ' Introduction to Programming
-
- ' Copyright 1986 Ford Software
-
- ' 4845 Willowbend Blvd.
- ' Houston, TX 77035
- ' (713) 721-5205
- ' CompuServe ppn: 71355,470
-
- 'This Tutorial may not be copied or distributed in any form - printed, on disk
- 'or electronically - without the express written permission of Ford Software.
- ' Unlicensed distribution is a violation of copyright law
- ' and may be subject to civil and criminal prosecution.
-
- 'QuickBASIC is a trademark of the Microsoft Corporation.
- 'Ford Software is not associated in any way with the Microsoft Corporation.
-
- '(Make sure NumLock is off and press PgDn)
- 'page 2 I N T R O D U C T I O N
-
- 'In this part of the QB2 tutorial, we are going to assume that you have never
- 'programmed before. The QuickBASIC manual is primarily a reference book, not
- 'a book to learn by. The value of this tutorial is that it is interactive
- 'You will be able to try things out as you go, which greatly speeds up the
- 'learning process. However, for maximum benefit, as points are covered here,
- 'you should also read about them in the QB2 manaul.
-
- 'Here are some basics you will need to start with:
- ' - When entering code, you can put each statement on a separate line, or you
- 'can combine several on the same line with a colon (:) in between statements.
- ' - The compile ignores anything following a single quote mark (').
- ' - In your programs, you will constantly be testing the relationship between
- 'things. Here are the relational symbols. "AND" and "OR" can be used to make
- 'multiple tests. = equal to
- ' < less than
- ' > greater than
- ' <> not equal to
- ' =< or <= less than or equal to
- ' => or >= greater than or equal to
- '(Press PgDn)
- 'page 3
- ' Contents
- ' page
- ' 4 What's a Program
- ' 4 What's a Variable
- ' 5 Integer Variables
- ' 6 Single Precision Variables
- ' 6 Double Precision Variables
- ' 6 String Variables
- ' 7 Declaring Variable Types
- ' 7 Review
- ' 8 Examples of Variable Types
- ' 9 Arrays
- ' 9 Loops
- ' 10 FOR-NEXT Loops
- ' 11 WHILE-WEND Loops
- ' 12 String Handling Commands
- ' 13 String Operators
- ' 14 Other String Commands
- '
- ' To search for specific text, use Ctrl-F.
- '(Press PgDn)
- 'page 4 What's a Program, a Variable?
-
- 'WHAT'S A PROGRAM?
- 'A program is a set of instructions to the computer. The compiler converts
- 'your English-like instructions to machine language the computer can under-
- 'stand. With any computer language, such as QB2, you must use the "vocabulary"
- 'and "sentence structure" of the language exactly as proscribed.
-
- 'In movies and TV shows, the hero sits at the computer and types something
- 'like "PRINT NAMES OF ALL PEOPLE WHO EAT QUICHE" and the computer scrolls a
- 'list across the screen, usually a letter at a time with an electronic beep
- 'as each letter is printed. Ah well, that's showbiz.
-
- 'In the real world of QB2, if you enter something like the line above, the
- 'computer will quickly and quietly display "0 0 0 0 0 0 0", because it thinks
- 'each of those words ("NAMES" through "QUICHE") is a numeric variable with a
- 'value of 0 which you have asked it to print.
-
- 'WHAT'S A VARIABLE?
- 'A variable is used to store data while a program is running. A variable can
- 'be almost any combination of letters and numbers, but if you want to be able
- ' (Press PgDn)
- 'page 5 Variable Types
-
- 'to remember when you look back at your program later what a variable is for,
- 'it's a good idea to use a variable name that will remind you.
-
- Example1:
- INPUT "What were total sales"; SALES
- INPUT "What is the sales tax rate"; TAX.RATE
- PRINT "Sales tax on sales of" SALES "is" SALES * TAX.RATE
- END' variables
-
- 'To run this example, just press Ctrl-R. The program should compile and then
- 'when you press Enter, it should run. If it does not, press Alt-F, L, Enter,
- 'and reload the program from the disk. If it still does not, go back to DOS
- 'and recopy the program from the distribution disk onto your work disk again.
-
- 'INTEGER VARIABLES...
- ' can hold numbers from -32,768 to +32,767 with no decimal places. Integers
- 'take up the least amount of space in memory, which makes them faster to
- 'manipulate as well. The sign for an integer is "%". Eg: X%=3
-
- '(PgDn)
- 'page 6 (Variable Types, Continued)
-
- 'SINGLE PRECISION VARIABLES...
- ' are precise to 7 digits total, on both sides of the decimal place. For
- 'example, 1234567 and 1234.567 are both single precision numbers. Beyond
- 'seven digits you start getting rounding errors. Single precision numbers take
- 'twice as much space as integers, but only half as much as double precision
- 'numbers. You always want to use the smallest varible type that will hold the
- 'number accurately. A single precision variable is denoted with a "!": X!=6.2
-
- 'DOUBLE PRECISION VARIABLES...
- ' are accurate to 16 digits, which is the most accuracy you can squeeze
- 'out of the system in BASIC. Like single precision, the number of digits is
- 'the total on both sides of the decimal place. A double precision variable is
- 'indicated with a "#". Eg: X#=123456.789
-
- 'STRING VARIABLES
- 'A string variable holds text. In the interpreter a string variable is limited
- 'to 255 characters. In the compiler, a string variable may hold up to 32,767
- 'characters. The symbol for a string variable is a "$". Eg: X$="Hello"
-
- '(PgDn)
- 'page 7 Declaring Variable Types
-
- 'DECLARING VARIABLE TYPES
- 'It is a nuisance to have to keep typing the symbol for the variable type
- 'every time you enter a variable name. The DEFINT, DEFSNG, DEFDBL and DEFSTR
- 'commands let you establish that any variable name starting with the specified
- 'letter is a particular type of variable. For example, DEFDBL S means that any
- 'variable starting with the letter S is a double precision variable. This
- 'declaration can be overridden by using the symbol. For example, SALES would
- 'be a double-precision variable and SONG$ would be a string variable.
-
- 'REVIEW
- ' X% is an integer that can hold numbers from -32,768 to +32,767, no decimals.
- ' X! is single precision and can hold numbers accurately to 7 digits.
- ' X# is double precision and accurate to 16 digits.
- ' X$ is a string variable that can hold up to 32,767 characters.
-
- 'PROGRAMMING TIP
- 'At the start of a program, you can declare all variables as integers and use
- 'the appropriate symbols to override this declaration for other variable types
- 'only when needed. This insures that you will use the faster, smaller integers
- 'whenever possible. (PgDn)
- 'page 8 Examples of Variable Types
-
- Example2: 'Remove the quote from the start of lines 3 and 4 before running.
- DEFINT A-Z 'To run this example, start by pressing Ctrl-PgUp to
- A=123: PRINT A 'get to the first line of this file. Edit that line
- 'B=123456: PRINT B 'to say "goto example2" and press Ctrl-R to run.
- 'C=12345675: PRINT C 'You will get a compile-time error message saying
- END '"Math overflow" for the preceeding line. Change the
- 'C's in that line to C# and press Ctrl-R. (You don't have to change the first
- 'line in the file again.) You still get the same error message. What's going
- 'on? Well, QB2 does not always point to the right line. The line above is also
- 'wrong. Change the B's in that line to B! and try again.
-
- 'Now let's try some cutting and pasting. Move to the first D in the next line:
- 'D="123456abc": PRINT D '(Press Alt-V, E, Enter to remove error message box.)
- 'With the NumLock off, press Shift-End. The line above should be highlighted.
- 'Press the Del key. Move the cursor to the "E" in the "END" statement in
- 'Example2 above and press Enter. Move up a row to the blank line and press
- 'the Ins key and the text you "cut" out should reappear. Press Ctrl-R to run
- 'this modified code. You will get a "Data type conflict" for trying to assign
- 'a text string to a non-string variable. Add the "$" symbols and try again.
- '(PgDn)
- 'page 9 Arrays, Loops
-
- 'ARRAYS...
- ' are what makes variables and programs really powerful. Thanks to spread-
- 'sheet programs, we now have a good analogy for arrays: rows and columns in
- 'the spreadsheet. A single array is like a single column of data in a spread-
- 'sheet. A two-element array is like multiple columns and rows. An example of
- 'a variable array would be SALES#(12,50) where the 12 would be the number of
- 'columns in a spreadsheet, perhaps a column for each month, and 50 would be
- 'the number of rows, perhaps a product number on each row.
-
- 'LOOPS...
- ' are what give arrays their power (and vice-versa). Let's say you wanted
- 'to increase sales for each item in a column by 20%. In a spreadsheet, you
- 'would have to go down the column multiplying each of the 50 amounts by 120%.
- 'With a FOR-NEXT loop in BASIC, you could just say
- 'FOR ROW=1 TO 50
- ' SALES#(COLUMN,ROW)=SALES#(COLUMN,ROW) * 1.2 'Some people get confused
- 'NEXT ' by the structure "x=x+3" because it is algebraically impossible.
- ' This is not algebra. This is shorthand for a language which says
- ' "set x equal to the old value of x plus 3"
- '(PgDn)
- 'page 10 FOR-NEXT Loops
-
- Example3:
- FOR I=6 TO 9 ' This example tells the computer to set the value of I to
- PRINT "I="I ' the value of the first number (6), then perform the action
- NEXT ' if I is less than or equal to the second number (9). I is
- 'incremented by 1 when NEXT is reached and tested again to see if it is still
- 'less than or equal to 9.
-
- FOR J=1 TO 8 STEP 3 ' You can also make the counter increment more than 1
- PRINT "J step" J ' by using the STEP command. In this example, J will
- NEXT ' increment by 3.
-
- FOR K=5 TO 3 STEP -1 ' You can also make the counter decrease instead of
- PRINT "K loop" K ' counting up by using a negative STEP amount. Here,
- NEXT ' K will start at 5 and decrement by 1.
- END
-
- 'To run these examples, press Ctrl-PgUp to get to the title screen, change the
- 'first line to "goto example3" and press Ctrl-R. Then return to this page.
-
- ' (PgDn)
- 'page 11 WHILE-WEND Loops
-
- 'WHILE-WEND LOOPS...
- ' repeat an action as long as the testing statment is true. In simplest form,
- 'a WHILE-WEND loop can be directly substituted for a FOR-NEXT loop:
-
- example4:
- I=6 ' Press PgUp to compare this loop to the first loop on the
- WHILE I <= 9 ' previous screen. It has exactly the same result, although
- PRINT "I=" I ' this method is quite a bit more cumbersome. For one thing,
- I=I+1 ' a FOR-NEXT loop increments the counter automatically. In
- WEND ' the WHILE-WEND loop, if you want a counter, you must set
- END ' it up and increment it yourself.
-
- 'WHILE-WEND loops are usually more efficient then FOR-NEXT loops when dealing
- 'with string variables, but we need to talk more about string variables first.
- 'In a textbook, we might have to go through a lot to illustrate the string-
- 'handling capabilities of BASIC. In this tutorial, however, all you have to do
- 'is press Ctrl-PgUp, change the line to "goto example5" and press Ctrl-R.
- '(Example 5 is on the next page.)
-
- '(PgDn)
- 'page 12 String-Handling Commands
-
- Example5:
- X$="QuickBASIC Compiler"
- PRINT " " X$
- PRINT "left 5 characters: " LEFT$(X$, 5)
- PRINT "left 10 characters: " LEFT$(X$,10)
- PRINT "6th thru 10th char.: " MID$(X$,6,5) 'start at char. 6, print 5 char.
- PRINT "6th thru last char.: " MID$(X$,6) 'start at char. 6, print the rest
- PRINT "right 5 characters: " RIGHT$(X$,5)
- END
-
- 'Now here is an example of how a WHILE-WEND loop can be used with a string.
- 'We are going to look for someone's last name in this next example:
- Example6:
- WHOLE.NAME$="Nelson Ford":I=1 'Ctrl-PgUp, "goto example6", Ctrl-R
- WHILE MID$(WHOLE.NAME$,I,1) <> " " 'to run this example.
- I=I+1
- WEND
- PRINT "Last name: " MID$(WHOLE.NAME$,I+1)
- END
- ' (PgDn)
- 'page 13 String Operators
-
- 'String Operators:
- ' Symbol Meaning Example
- ' < precedes alphabetically "A" < "B"
- ' > follows alphabetically "AZ > "AX"
- ' = equals X$ = "A"
- ' <> not equal "A" <> "a"
- ' + join two strings together X$ = "A"+"B"
-
- 'You can change just part of string by using MID$:
- example7:
- A$="I think IBM PCs are great."
- B$=" Clones" 'Try leaving out the space before the C and run it.
- X=INSTR(A$, "IBM") 'Remember the INSTR function?
- PRINT A$
- MID$(A$,X)=B$ 'Replace "IBM PCs" with " Clones"
- PRINT A$
- END
- 'Only MID$ can be used in this way, not LEFT$ or RIGHT$.
- 'See "MID$ Statement" in the manual for other restrictions.
- '(PgDn)
- 'page 14 Other String Commands
-
- 'INSTR and LEN
-
- example8:
- A$="12345abc345xyz": PRINT "A$=" A$
- B$="345": PRINT "B$=" B$
- 'INSTR is the character number in the first string where the second string
- ' begins. If the second string is not found, INSTR is 0.
- X=INSTR(A$, B$)
- PRINT "Starting @ char. #" X, MID$(A$, X, 6)
- 'You can specify a point for the search for a match to start:
- Y=INSTR( X+1, A$, B$)
- ' -the starting point is the start of the first "345" + one
- PRINT "Starting @ char. #" Y, MID$(A$, Y)
-
- 'LEN returns the length of a string:
- PRINT "A$ is" LEN(A$) "char. long"
- END
-
-
- ' end of lesson 2
-
-
-
- 'end of file. Press PgUp.
-
-